package com.excel.poi.ali.inter;

import com.excel.poi.ali.*;
import lombok.Data;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zw
 * @Description 解析内容相关
 * @create /2022/3/25
 */
@Data
public class AnalysisContextImpl  {
    private Class c1;
    private SheetParam currentSheet;

    private InputStream inputStream;

    private AnalysisEventListener eventListener;
   //当前行 从0 开始
    private Integer currentRowNum;
    //每个sheet多少行
    private Integer totalCount;

    private ExcelHeadProperty excelHeadProperty;

    private boolean trim;

    private boolean use1904WindowDate = false;

    private Object currentRowAnalysisResult;

    public AnalysisContextImpl(InputStream inputStream, Class c1,AnalysisEventListener listener, boolean trim) {
        this.eventListener = listener;
        this.inputStream = inputStream;
        this.trim = trim;
        this.c1=c1;
    }

    public void setCurrentSheet(SheetParam currentSheet) throws Exception {
        cleanCurrentSheet();
        this.currentSheet = currentSheet;
        if (currentSheet.getClazz() != null) {
            buildExcelHeadProperty(currentSheet.getClazz(), null);
        }
    }

    private void cleanCurrentSheet() {
        this.currentSheet = null;
        this.excelHeadProperty = null;
        this.totalCount = 0;
        this.currentRowAnalysisResult = null;
        this.currentRowNum =0;
    }

    /**
     * 构建
     * @param clazz
     * @param headOneRow
     */
    public void buildExcelHeadProperty(Class<?> clazz, List<String> headOneRow) throws Exception {
        if (this.excelHeadProperty == null && (clazz != null || headOneRow != null)) {
            this.excelHeadProperty = new ExcelHeadProperty(clazz, new ArrayList<List<String>>());
        }
        if (this.excelHeadProperty.getHead() == null && headOneRow != null) {
            this.excelHeadProperty.appendOneRow(headOneRow);
        }
        if(null==clazz&&null!=headOneRow&&headOneRow.size()>0){
            Map<String, Field> fieldMap = excelHeadProperty.getFieldMap();
            List<String> mustListCopy = new ArrayList<>();
            mustListCopy.addAll(excelHeadProperty.getMustList());
            Map<Integer, Field> linkFieldMap = new HashMap<Integer, Field>();
            int i = 0;
            for (String key : headOneRow) {
              if(fieldMap.containsKey(key)){
                  linkFieldMap.put(i,fieldMap.get(key));
              }
                if(mustListCopy.contains(key)){
                    mustListCopy.remove(key);
                }
              //放在外
                i++;
            }
            if(mustListCopy.size()>0){
             throw new Exception("excel头缺少必要值:"+mustListCopy.toString());
            }
            this.excelHeadProperty.setLinkFieldMap(linkFieldMap);
        }


    }

    public boolean trim() {
        return this.trim;
    }
      /*  public Object getCurrentRowAnalysisResult() {
        return currentRowAnalysisResult;
    }*/
/*    public void setUse1904WindowDate(boolean use1904WindowDate) {
        this.use1904WindowDate = use1904WindowDate;
    }*/

   /* public SheetParam getCurrentSheet() {
        return currentSheet;
    }*/

    /*public InputStream getInputStream() {
        return inputStream;
    }*/

    /*public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }*/

/*    public AnalysisEventListener getEventListener() {
        return eventListener;
    }

    public void setEventListener(AnalysisEventListener eventListener) {
        this.eventListener = eventListener;
    }

    public Integer getCurrentRowNum() {
        return this.currentRowNum;
    }

    public void setCurrentRowNum(Integer row) {
        this.currentRowNum = row;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public ExcelHeadProperty getExcelHeadProperty() {
        return this.excelHeadProperty;
    }*/

 /*   public void interrupt() throws Exception {
        throw new Exception("interrupt error");
    }*/

   /* public boolean use1904WindowDate() {
        return use1904WindowDate;
    }*/

/*    public void setCurrentRowAnalysisResult(Object currentRowAnalysisResult) {
        this.currentRowAnalysisResult = currentRowAnalysisResult;
    }*/
}
